var LEFT = 0;
var RIGHT = 1;
var DOWN = 2;
var NONE = 3;

var CCW = "counterclockwise";
var CW = "clockwise";

var BRCK_O = [[[1, 1], [1, 1]]];
var BRCK_T = [[[2, 2, 2], [0, 2, 0], [0, 0, 0]], [[2, 0], [2, 2], [2, 0]], [[0, 0, 0], [0, 2, 0], [2, 2, 2]], [[0, 0, 2], [0, 2, 2], [0, 0, 2]]];
var BRCK_I = [[[0, 3], [0, 3], [0, 3], [0, 3]], [[0, 0, 0, 0], [3, 3, 3, 3]]];
var BRCK_J = [[[0, 4, 4], [0, 4, 0], [0, 4, 0]], [[4, 0, 0], [4, 4, 4], [0, 0, 0]], [[0, 4, 0], [0, 4, 0], [4, 4, 0]], [[0, 0, 0], [4, 4, 4], [0, 0, 4]]];
var BRCK_L = [[[0, 5, 0], [0, 5, 0], [0, 5, 5]], [[0, 0, 5], [5, 5, 5], [0, 0, 0]], [[5, 5, 0], [0, 5, 0], [0, 5, 0]], [[0, 0, 0], [5, 5, 5], [5, 0, 0]]];
var BRCK_S = [[[0, 6, 0], [0, 6, 6], [0, 0, 6]], [[0, 0, 0], [0, 6, 6], [6, 6, 0]], [[0, 6, 0], [0, 6, 6], [0, 0, 6]], [[0, 0, 0], [0, 6, 6], [6, 6, 0]]];
var BRCK_Z = [[[0, 7, 0], [7, 7, 0], [7, 0, 0]], [[0, 0, 0], [7, 7, 0], [0, 7, 7]], [[0, 7, 0], [7, 7, 0], [7, 0, 0]], [[0, 0, 0], [7, 7, 0], [0, 7, 7]]];
var BRICKS = [BRCK_O, BRCK_T, BRCK_I, BRCK_J, BRCK_L, BRCK_S, BRCK_Z];

var Brick = function() {
    var type;
    var x;
    var y;
    var state;
    var mask;
    
    function init() {
        // choose a random brick
        var rand_brick = Math.floor(Math.random() * BRICKS.length);
        type = BRICKS[rand_brick];
        
        // choose a random rotation state
        var state = Math.floor(Math.random() * type.length);
        
        // set the brick's mask
        mask = type[state];
        
        // initial position
        x = 0;
        y = 0;
    }
    
    function move(direction) {
        switch (direction) {
            case LEFT:
                x--;
                break;

            case RIGHT:
                x++;
                break;

            case DOWN:
                y++;
                break;

            default:
                // invalid direction
                break;
        }
    }
    
    function rotate(rotation) {
        rotation = rotation || CCW;
        
        if (rotation == CCW) {
            // check if the brick's state is the last state in the brick's mask array
            if (type.length - 1 == state) {
                state = 0;
                mask = type[state];
            } else {
                mask = type[++state];
            }
        } else {
            // check if the brick's state is the first state in the brick's mask array
            if (state == 0) {
                state = type.length - 1;
                mask = type[state];
            } else {
                mask = type[--state];
            }
        }
        
        
    }
    
    init();
    return {
        init: init,
        move: move,
        rotate: rotate,
        getMask : function() { return mask; },
        setMask : function(val) { mask = val; }
    };
    
};


function main() {
    var b = new Brick();
    $brick = $('<div style="color:orange"></div>');
    
    for (i = 0; i < b.getMask()[0].length; i++) {
        for (j = 0; j < b.getMask().length; j++) {
            var element = b.getMask()[j][i];
            if (element > 0) $brick.append("&#9632;");
        }
        $brick.append('<br />');
    }
    
    $("#game").append($brick);
    
}

$(main);